package com.toman.templateActivity.controler;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ContextThemeWrapper;
import android.view.Gravity;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Adapter;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;

import com.toman.MyApplication;
import com.toman.R;
import com.toman.Util.DefaultAcitonCallback;
import com.toman.Util.DensityUtil;
import com.toman.Util.DialogUtils;
import com.toman.Util.TimeUtil;
import com.toman.Util.ToastUtil;
import com.toman.Util.ViewUtil;
import com.toman.bean.ItemListBean;
import com.toman.bean.TemplateInfBean;
import com.toman.bean.TemplateInfBeanPreHandler;
import com.toman.widget.ClearEditText;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 邹苏隆 on 2019/1/9.
 * description：
 */
public class TempView {
    private List<TemplateInfBean> list = new ArrayList<>();
    //    子布局
    private Context context;
    LinearLayout ll;
    boolean edit = false;

    private void addEditText(TemplateInfBean bean) {

        RelativeLayout.LayoutParams rlParams0 =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT, DensityUtil.dip2px(context, 48)
                );
        RelativeLayout.LayoutParams rlParams =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams rlParams2 =
                new RelativeLayout.LayoutParams(
                        DensityUtil.dip2px(context, 215),
                        DensityUtil.dip2px(context, 34));
        if (bean.isMarginTop()) {
            rlParams0.setMargins(0, DensityUtil.dip2px(context, 6), 0, 0);
        }

        RelativeLayout relativeLayout = new RelativeLayout(new ContextThemeWrapper(context, R.style.rl_edit_inf));
        TextView textView = new TextView(new ContextThemeWrapper(context, R.style.tv_edit_left));
        textView.setText(bean.getTitle());
        textView.setId(R.id.tv_code_title);
//        设置必要性
        necessary(bean, textView);
        rlParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        rlParams.addRule(RelativeLayout.CENTER_VERTICAL);
        textView.setPadding(DensityUtil.dip2px(context, 14), 0, 0, 0);
        relativeLayout.addView(textView, rlParams);

//        ClearEditText editText = new ClearEditText(new ContextThemeWrapper(context, R.style.et_edit_right));
        ClearEditText editText = new ClearEditText(context);
        editText.setTextSize(16);

        editText.setId(R.id.tv_code_content);
        editText.setPadding(DensityUtil.dip2px(context, 14), 0, DensityUtil.dip2px(context, 6), 0);
        editText.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        rlParams2.addRule(RelativeLayout.CENTER_VERTICAL);
        editText.setText(bean.getContent());
        editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
        editText.setHint("请输入");

        editText.setInputType(bean.getInputType());

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                bean.setResult(editText.getText().toString());
                bean.setContent(editText.getText().toString());
            }
        });
        ViewUtil.unedit(editText);

        if (edit) {
            if (bean.isEdit()) {
                ViewUtil.edit(editText);
            }
        } else {
            ViewUtil.unedit(editText);
        }
        rlParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        rlParams2.addRule(RelativeLayout.CENTER_VERTICAL);
        rlParams2.setMargins(0, 0, DensityUtil.dip2px(context, 14), 0);
        relativeLayout.addView(editText, rlParams2);
        ll.addView(relativeLayout, rlParams0);
        addLine(bean, relativeLayout);


    }

    private void addEditMoreText(TemplateInfBean bean) {
        RelativeLayout.LayoutParams rlParams0 =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT, DensityUtil.dip2px(context, 80)
                );
        RelativeLayout.LayoutParams rlParams =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams rlParams2 =
                new RelativeLayout.LayoutParams(
                        DensityUtil.dip2px(context, 215),
                        DensityUtil.dip2px(context, 70));
        if (bean.isMarginTop()) {
            rlParams0.setMargins(0, DensityUtil.dip2px(context, 6), 0, 0);
        }

        RelativeLayout relativeLayout = new RelativeLayout(new ContextThemeWrapper(context, R.style.rl_edit_inf));
        TextView textView = new TextView(new ContextThemeWrapper(context, R.style.tv_edit_left));
        textView.setText(bean.getTitle());
        textView.setId(R.id.tv_code_title);
        necessary(bean, textView);

        rlParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        rlParams.addRule(RelativeLayout.CENTER_VERTICAL);
        textView.setPadding(DensityUtil.dip2px(context, 14), 0, 0, 0);
        relativeLayout.addView(textView, rlParams);

//        ClearEditText editText = new ClearEditText(new ContextThemeWrapper(context, R.style.et_edit_right));
        ClearEditText editText = new ClearEditText(context);
//editText.setShakeAnimation();
        editText.setTextSize(16);

        editText.setId(R.id.tv_code_content);
        editText.setPadding(DensityUtil.dip2px(context, 14), 0, DensityUtil.dip2px(context, 6), 0);
        editText.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                return true;
            }
        });
        rlParams2.addRule(RelativeLayout.CENTER_VERTICAL);
        editText.setText(bean.getContent());
        editText.setHint("请输入");
        editText.setTextSize(16);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                bean.setResult(editText.getText().toString());
                bean.setContent(editText.getText().toString());
            }
        });
        ViewUtil.unedit(editText);

        if (edit) {
            if (bean.isEdit()) {
                ViewUtil.edit(editText);
            }
        } else {
            ViewUtil.unedit(editText);
        }
        rlParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
//        rlParams2.addRule(RelativeLayout.CENTER_VERTICAL);
        rlParams2.setMargins(0, 0, DensityUtil.dip2px(context, 14), 0);
        relativeLayout.addView(editText, rlParams2);
        ll.addView(relativeLayout, rlParams0);
        addLine(bean, relativeLayout);


    }


    private void addClickText(TemplateInfBean bean) {
        RelativeLayout.LayoutParams rlParams0 =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,  RelativeLayout.LayoutParams.WRAP_CONTENT
                );
        if (bean.isMarginTop()) {
            rlParams0.setMargins(0, DensityUtil.dip2px(context, 6), 0, 0);
        }

        RelativeLayout.LayoutParams rlParams =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams rlParams2 =
                new RelativeLayout.LayoutParams(
                        DensityUtil.dip2px(context, 215),
                        RelativeLayout.LayoutParams.WRAP_CONTENT);


        RelativeLayout relativeLayout = new RelativeLayout(new ContextThemeWrapper(context, R.style.rl_edit_inf));
        relativeLayout.setMinimumHeight( DensityUtil.dip2px(context, 48));

        TextView textView = new TextView(new ContextThemeWrapper(context, R.style.tv_edit_left));
        textView.setText(bean.getTitle());
        necessary(bean, textView);
        textView.setId(R.id.tv_code_title);
        rlParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        rlParams.addRule(RelativeLayout.CENTER_VERTICAL);
        textView.setPadding(DensityUtil.dip2px(context, 14), 0, 0, 0);
        relativeLayout.addView(textView, rlParams);

//        TextView editText =new TextView(context);
        TextView editText = new TextView(new ContextThemeWrapper(context, R.style.tv_inf_right));

        editText.setId(R.id.tv_code_content);
        editText.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
        editText.setPadding(DensityUtil.dip2px(context, 14), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 6), 0);
        rlParams2.addRule(RelativeLayout.CENTER_VERTICAL);
        rlParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        rlParams2.setMargins(0, 0, DensityUtil.dip2px(context, 14), 0);

        editText.setText(bean.getContent());
        editText.setHint("未填写");

        if (edit) {
            if (bean.isEdit()) {
                ViewUtil.click(editText);
            }
        } else {
            ViewUtil.unclick(editText);
        }
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!edit) {
                    return;
                }

                if (null == bean.getItemListBeans())

                {
                    DialogUtils.showDialog(context,bean, new DefaultAcitonCallback() {
                        @Override
                        public void run(ItemListBean bean1, Dialog dialog) {
                            if (preHandler(bean)) return;
                            bean.setResult(bean1.getID());
                            bean.setContent(bean1.getName());
                            bean.setList_get(bean1.getList_get());
                            editText.setText(bean1.getName());
                            bean.trigger();
                            dialog.dismiss();
                        }
                    });
                } else {

                    DialogUtils.showDialog(context, bean.getTitle(), bean.getItemListBeans(), new DefaultAcitonCallback() {
                        @Override
                        public void run(ItemListBean bean1, Dialog dialog) {
                            if (preHandler(bean)) return;
                            bean.setResult(bean1.getID());
                            bean.setContent(bean1.getName());
                            editText.setText(bean1.getName());
                            bean.trigger();
                            dialog.dismiss();

                        }
                    });

                }
            }
        });
        rlParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        rlParams2.addRule(RelativeLayout.CENTER_VERTICAL);
        relativeLayout.addView(editText, rlParams2);
        ll.addView(relativeLayout, rlParams0);
        addLine(bean, relativeLayout);


    }

    private boolean preHandler(TemplateInfBean bean) {
        if (bean.getPreHandlers() != null) {
            for (TemplateInfBeanPreHandler preHandler : bean.getPreHandlers()) {
                if (preHandler.preHandle()) {

                } else {
                    return true;
                }
            }
        }
        return false;
    }

    private void addMutiClickText(TemplateInfBean bean) {
        RelativeLayout.LayoutParams rlParams0 =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT
                );
        if (bean.isMarginTop()) {
            rlParams0.setMargins(0, DensityUtil.dip2px(context, 6), 0, 0);
        }
        RelativeLayout.LayoutParams rlParams =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams rlParams2 =
                new RelativeLayout.LayoutParams(
                        DensityUtil.dip2px(context, 215),
                        RelativeLayout.LayoutParams.WRAP_CONTENT);


        RelativeLayout relativeLayout = new RelativeLayout(new ContextThemeWrapper(context, R.style.rl_edit_inf));
        TextView textView = new TextView(new ContextThemeWrapper(context, R.style.tv_edit_left));
        textView.setText(bean.getTitle());
        textView.setId(R.id.tv_code_title);
        necessary(bean, textView);

        rlParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        rlParams.addRule(RelativeLayout.CENTER_VERTICAL);
        textView.setPadding(DensityUtil.dip2px(context, 14), 0, 0, 0);
        relativeLayout.addView(textView, rlParams);

//        TextView editText =new TextView(context);
        TextView editText = new TextView(new ContextThemeWrapper(context, R.style.tv_inf_right));
        editText.setMinLines(2);
        editText.setId(R.id.tv_code_content);
        editText.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
        editText.setPadding(DensityUtil.dip2px(context, 14), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 6), 0);
        rlParams2.addRule(RelativeLayout.CENTER_VERTICAL);
        rlParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        rlParams2.setMargins(0, 0, DensityUtil.dip2px(context, 14), 0);

        editText.setText(bean.getContent());
        editText.setHint("未填写");

        if (edit) {
            if (bean.isEdit()) {
                ViewUtil.click(editText);
            }
        } else {
            ViewUtil.unclick(editText);
        }
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!edit) {
                    return;
                }
                if (preHandler(bean)) return;
                DialogUtils.showMutiDialog(context,editText.getText().toString(), bean, new DefaultAcitonCallback() {
                    @Override
                    public void run(ItemListBean bean1, Dialog dialog) {
                        bean.setResult(bean1.getID());
                        bean.setContent(bean1.getName());
                        editText.setText(bean1.getName());
                        bean.trigger();
//                        dialog.dismiss();
                    }
                });
            }
        });
        rlParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        rlParams2.addRule(RelativeLayout.CENTER_VERTICAL);
        relativeLayout.addView(editText, rlParams2);
        ll.addView(relativeLayout, rlParams0);
        addLine(bean, relativeLayout);


    }

    private void addDateText(TemplateInfBean bean) {
        RelativeLayout.LayoutParams rlParams0 =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT, DensityUtil.dip2px(context, 48)
                );
        if (bean.isMarginTop()) {
            rlParams0.setMargins(0, DensityUtil.dip2px(context, 6), 0, 0);
        }
        RelativeLayout.LayoutParams rlParams =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams rlParams2 =
                new RelativeLayout.LayoutParams(
                        DensityUtil.dip2px(context, 215),
                        DensityUtil.dip2px(context, RelativeLayout.LayoutParams.WRAP_CONTENT));


        RelativeLayout relativeLayout = new RelativeLayout(new ContextThemeWrapper(context, R.style.rl_edit_inf));
        TextView textView = new TextView(new ContextThemeWrapper(context, R.style.tv_edit_left));
        textView.setText(bean.getTitle());
        textView.setId(R.id.tv_code_title);
        necessary(bean, textView);

        rlParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        rlParams.addRule(RelativeLayout.CENTER_VERTICAL);
        textView.setPadding(DensityUtil.dip2px(context, 14), 0, 0, 0);
        relativeLayout.addView(textView, rlParams);

//        TextView editText =new TextView(context);
        TextView editText = new TextView(new ContextThemeWrapper(context, R.style.tv_inf_right));

        editText.setId(R.id.tv_code_content);
        editText.setMinLines(2);
        editText.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
        editText.setPadding(DensityUtil.dip2px(context, 14), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 6), 0);
        rlParams2.addRule(RelativeLayout.CENTER_VERTICAL);
        rlParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        rlParams2.setMargins(0, 0, DensityUtil.dip2px(context, 14), 0);

        editText.setText(bean.getContent());
        editText.setHint("请选择");

        if (edit) {
            if (bean.isEdit()) {
                ViewUtil.click(editText);
            }
        } else {
            ViewUtil.unclick(editText);
        }
        editText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!edit) {
                    return;
                }
                switch (bean.getTimeEnum()) {
                    case TIME:
                        getTime(context, editText, bean);
                        ;
                        break;
                    case DATE:
                        getDate(context, editText, bean);
                        ;
                        break;
                    case BOTH:
                        getDateTime(context, editText, bean);
                        ;
                        break;
                }
            }

        });
        rlParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        rlParams2.addRule(RelativeLayout.CENTER_VERTICAL);
        relativeLayout.addView(editText, rlParams2);
        ll.addView(relativeLayout, rlParams0);
        addLine(bean, relativeLayout);


    }

    private void addText(TemplateInfBean bean) {
        RelativeLayout.LayoutParams rlParams0 =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT, DensityUtil.dip2px(context, 48)
                );
        if (bean.isMarginTop()) {
            rlParams0.setMargins(0, DensityUtil.dip2px(context, 6), 0, 0);
        }
        RelativeLayout.LayoutParams rlParams =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams rlParams2 =
                new RelativeLayout.LayoutParams(
                        DensityUtil.dip2px(context, 215),
                        DensityUtil.dip2px(context, 34));


        RelativeLayout relativeLayout = new RelativeLayout(new ContextThemeWrapper(context, R.style.rl_edit_inf));
        TextView textView = new TextView(new ContextThemeWrapper(context, R.style.tv_edit_left));
        textView.setText(bean.getTitle());
        textView.setId(R.id.tv_code_title);
        necessary(bean, textView);

        rlParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
        rlParams.addRule(RelativeLayout.CENTER_VERTICAL);
        textView.setPadding(DensityUtil.dip2px(context, 14), 0, 0, 0);
        relativeLayout.addView(textView, rlParams);

        TextView editText = new TextView(new ContextThemeWrapper(context, R.style.tv_inf_right));
        editText.setId(R.id.tv_code_content);
        editText.setPadding(DensityUtil.dip2px(context, 14), DensityUtil.dip2px(context, 6), DensityUtil.dip2px(context, 6), 0);
        rlParams2.addRule(RelativeLayout.CENTER_VERTICAL);
        editText.setText(bean.getContent());
        editText.setHint("未填写");
        rlParams2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        rlParams2.addRule(RelativeLayout.CENTER_VERTICAL);
        rlParams2.setMargins(0, 0, DensityUtil.dip2px(context, 14), 0);

        relativeLayout.addView(editText, rlParams2);
        ll.addView(relativeLayout, rlParams0);
        addLine(bean, relativeLayout);

    }


    private void addLine(TemplateInfBean bean, RelativeLayout relativeLayout) {
        if (list.get(list.size() - 1) == bean) {
            return;
        }
        int index = list.indexOf(bean);
        if (list.get(index + 1).isMarginTop()) {
            return;
        }
        RelativeLayout.LayoutParams rlParams0 =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 1)
                );

        View line = new View(context);
        line.setBackgroundColor(Color.parseColor("#e6e4e4"));
        rlParams0.setMargins(DensityUtil.dip2px(context, 14), 0, DensityUtil.dip2px(context, 14), 0);
        rlParams0.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        relativeLayout.addView(line, rlParams0);
    }

    private void addLine(TemplateInfBean bean, LinearLayout relativeLayout) {
        if (list.get(list.size() - 1) == bean) {
            return;
        }
        int index = list.indexOf(bean);
        if (list.get(index + 1).isMarginTop()) {
            return;
        }
        RelativeLayout.LayoutParams rlParams0 =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 1)
                );

        View line = new View(context);
        line.setBackgroundColor(Color.parseColor("#e6e4e4"));
        rlParams0.setMargins(DensityUtil.dip2px(context, 14), 0, DensityUtil.dip2px(context, 14), 0);
        rlParams0.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        relativeLayout.addView(line, rlParams0);

    }

    private void addLine(TemplateInfBean bean) {
        if (list.get(list.size() - 1) == bean) {
            return;
        }
        int index = list.indexOf(bean);
        if (list.get(index + 1).isMarginTop()) {
            return;
        }
        RelativeLayout.LayoutParams rlParams0 =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 1)
                );

        View line = new View(context);
        line.setBackgroundColor(Color.parseColor("#e6e4e4"));
        rlParams0.setMargins(DensityUtil.dip2px(context, 14), 0, DensityUtil.dip2px(context, 14), 0);

        ll.addView(line, rlParams0);

    }

    private void addPhoto(TemplateInfBean bean) {

        if (edit) {
            addTop(bean);
            bean.setCamera(ViewUtil.createPhoto(bean.getTitle(), bean.getProblemImageList(), bean.getActivity(), ll, !bean.isEdit()));
        } else {
            if (bean.getProblemImageList() == null) {
                return;
            }
            addTop(bean);
            bean.setCamera(ViewUtil.createPhoto(bean.getTitle(), bean.getProblemImageList(), bean.getActivity(), ll, true));
        }
        addLine(bean);
    }

    private void addTop(TemplateInfBean bean) {
        RelativeLayout.LayoutParams rlParams0 =
                new RelativeLayout.LayoutParams(
                        RelativeLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 6)
                );

        View line = new View(context);
        if (bean.isMarginTop()) {
            ll.addView(line, rlParams0);
        }


    }
    public void getDateTime(final Context mContext, final TextView textView, TemplateInfBean bean) {
        View date_time_picker = View.inflate(mContext, R.layout.date_time_picker, null);
        final DatePicker datePicker = (DatePicker) date_time_picker.findViewById(R.id.data_picker);
        final TimePicker timePicker = (TimePicker) date_time_picker.findViewById(R.id.timer_picker);
        timePicker.setVisibility(View.VISIBLE);

        timePicker.setIs24HourView(true);
        //   Build   DateTimeDialog
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(date_time_picker);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int month = datePicker.getMonth() + 1;
                String dateStr = datePicker.getYear() + "-" + month + "-" + datePicker.getDayOfMonth();
                int currentMinute = timePicker.getCurrentMinute();
                String curMinute = "";
                if (currentMinute < 10) {
                    curMinute = "0" + currentMinute;
                } else {
                    curMinute = String.valueOf(currentMinute);
                }
                int currentHour = timePicker.getCurrentHour();
                String curHour = "";
                if (currentHour < 10) {
                    curHour = "0" + currentHour;
                } else {
                    curHour = String.valueOf(currentHour);
                }

                String timeStr = curHour + ":" + curMinute + ":00";
                if (!TimeUtil.compareNowTime(dateStr + " " + timeStr)) {
                    ToastUtil.makeText(mContext, "必须晚于现在的时间");
                    return;
                }

                textView.setText(dateStr + " " + timeStr);
                bean.setContent(textView.getText().toString());
                bean.setResult(textView.getText().toString());
            }
        });
        builder.show();
    }

    public void getDate(final Context mContext, final TextView textView, TemplateInfBean bean) {
        View date_time_picker = View.inflate(mContext, R.layout.date_picker, null);
        final DatePicker datePicker = (DatePicker) date_time_picker.findViewById(R.id.data_picker);


        //   Build   DateTimeDialog
        final AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(date_time_picker);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int month = datePicker.getMonth() + 1;
                String m, d;
                if (month < 10) {
                    m = "0" + month;
                } else {
                    m = "" + month;
                }
                if (datePicker.getDayOfMonth() < 10) {
                    d = "0" + datePicker.getDayOfMonth();
                } else {
                    d = "" + datePicker.getDayOfMonth();
                }
                String dateStr = datePicker.getYear() + "-" + m + "-" + d;
                textView.setText(dateStr);
                bean.setContent(textView.getText().toString());
                bean.setResult(textView.getText().toString());
            }
        });
        builder.show();


    }

    //    选择时间
    public void getTime(Context mContext, final TextView textView, TemplateInfBean bean) {
        View date_time_picker = View.inflate(mContext, R.layout.time_picker, null);
        final TimePicker timePicker = (TimePicker) date_time_picker.findViewById(R.id.timer_picker);

        timePicker.setIs24HourView(true);
        //   Build   DateTimeDialog
        AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
        builder.setView(date_time_picker);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                int currentMinute = timePicker.getCurrentMinute();
                String curMinute = "";
                if (currentMinute < 10) {
                    curMinute = "0" + currentMinute;
                } else {
                    curMinute = String.valueOf(currentMinute);
                }
                int currentHour = timePicker.getCurrentHour();
                String curHour = "";
                if (currentHour < 10) {
                    curHour = "0" + currentHour;
                } else {
                    curHour = String.valueOf(currentHour);
                }
                String timeStr = curHour + ":" + curMinute;
                ToastUtil.print(timeStr);
                textView.setText(timeStr);
                bean.setResult(textView.getText().toString());
                bean.setContent(textView.getText().toString());
            }
        });
        builder.show();


    }

    public void necessary(TemplateInfBean bean, TextView... views) {
        if (bean.isNecessary() && edit)
            for (TextView view : views) {
                MyApplication myApplication;
                myApplication = MyApplication.getInstance();
                Drawable drawable = myApplication.getResources().getDrawable(R.mipmap.required);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
//            view.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
//            view.setCompoundDrawablesWithIntrinsicBounds(null,null,drawable,null);
                view.setCompoundDrawables(null, null, drawable, null);

//            view.setTextAppearance(myApplication, R.style.et_edit_right_new);

            }
    }



    public void createView(TemplateInfBean bean,boolean edit,List<TemplateInfBean > list,LinearLayout ll,Context context) {
        this.edit=edit;
        this.list=list;
        this.ll=ll;
this.context=context;
        switch (bean.getType()) {
            case EDIT:
                addEditText(bean);
                break;
            case TEXT:
                addText(bean);
                break;

            case CLICK:
                addClickText(bean);

                break;
            case MUTICLICK:
                addMutiClickText(bean);

                break;
            case EDITMORE:
                addEditMoreText(bean);
                break;

            case PHOTO:
                addPhoto(bean);
                break;

            case DATE:
                addDateText(bean);
                break;

        }
    }
    /*比较大小*/
    public static boolean compareString(Context context, TemplateInfBean... views) {
        if (views.length != 2) {
            return true;
        }
        for (int i = 0; i < views.length; i++) {
            if (Integer.parseInt(views[0].getResult()) < Integer.parseInt(views[1].getResult())) {
                ToastUtil.makeText(context, views[0].getTitle() + "不得小于" + views[1].getTitle());
                return true;
            }
        }
        return false;

    }

}